/* {[The file is published on the basis of YetiForce Public License 5.0 that can be found in the following directory: licenses/LicenseEN.txt or yetiforce.com]} */
/*globals jQuery, define, exports, require, document */
(function (factory) {
	'use strict';
	if (typeof define === 'function' && define.amd) {
		define('jstree.category', ['jquery', 'jstree'], factory);
	} else if (typeof exports === 'object') {
		factory(require('jquery'), require('jstree'));
	} else {
		factory(jQuery, jQuery.jstree);
	}
})(function ($, jstree) {
	'use strict';
	var instance_counter = 0,
		ccp_node = false,
		ccp_mode = false,
		ccp_inst = false,
		themes_loaded = [],
		document = window.document, // local variable is always faster to access then a global
		_temp1,
		_temp2;

	var _i = document.createElement('I');
	_i.className = 'jstree-icon jstree-checkbox';
	_i.setAttribute('role', 'presentation');
	$.jstree.plugins.checkbox = function (options, parent) {
		this.bind = function () {
			parent.bind.call(this);
			this._data.checkbox.uto = false;
			this._data.checkbox.selected = [];
			this.element
				.on(
					'init.jstree',
					$.proxy(function () {
						this._data.checkbox.visible = this.settings.checkbox.visible;
						if (!this.settings.checkbox.keep_selected_style) {
							this.element.addClass('jstree-checkbox-no-clicked');
						}
						if (this.settings.checkbox.tie_selection) {
							this.element.addClass('jstree-checkbox-selection');
						}
					}, this)
				)
				.on(
					'loading.jstree',
					$.proxy(function () {
						this[this._data.checkbox.visible ? 'show_checkboxes' : 'hide_checkboxes']();
					}, this)
				);
			if (this.settings.checkbox.cascade.indexOf('undetermined') !== -1) {
				this.element.on(
					'changed.jstree uncheck_node.jstree check_node.jstree uncheck_all.jstree check_all.jstree move_node.jstree copy_node.jstree redraw.jstree open_node.jstree',
					$.proxy(function () {
						// only if undetermined is in setting
						if (this._data.checkbox.uto) {
							clearTimeout(this._data.checkbox.uto);
						}
						this._data.checkbox.uto = setTimeout($.proxy(this._undetermined, this), 50);
					}, this)
				);
			}
			if (!this.settings.checkbox.tie_selection) {
				this.element.on(
					'model.jstree',
					$.proxy(function (e, data) {
						var m = this._model.data,
							dpc = data.nodes,
							i,
							j;
						for (i = 0, j = dpc.length; i < j; i++) {
							m[dpc[i]].state.checked =
								m[dpc[i]].state.checked ||
								(m[dpc[i]].original && m[dpc[i]].original.state && m[dpc[i]].original.state.checked);
							if (m[dpc[i]].state.checked) {
								this._data.checkbox.selected.push(dpc[i]);
							}
						}
					}, this)
				);
			}
			if (
				this.settings.checkbox.cascade.indexOf('up') !== -1 ||
				this.settings.checkbox.cascade.indexOf('down') !== -1
			) {
				this.element
					.on(
						'model.jstree',
						$.proxy(function (e, data) {
							var m = this._model.data,
								p = m[data.parent],
								dpc = data.nodes,
								chd = [],
								c,
								i,
								j,
								k,
								l,
								tmp,
								s = this.settings.checkbox.cascade,
								t = this.settings.checkbox.tie_selection;

							if (s.indexOf('down') !== -1) {
								// apply down
								if (p.state[t ? 'selected' : 'checked']) {
									for (i = 0, j = dpc.length; i < j; i++) {
										m[dpc[i]].state[t ? 'selected' : 'checked'] = true;
									}
									this._data[t ? 'core' : 'checkbox'].selected =
										this._data[t ? 'core' : 'checkbox'].selected.concat(dpc);
								} else {
									for (i = 0, j = dpc.length; i < j; i++) {
										if (m[dpc[i]].state[t ? 'selected' : 'checked']) {
											for (k = 0, l = m[dpc[i]].children_d.length; k < l; k++) {
												m[m[dpc[i]].children_d[k]].state[t ? 'selected' : 'checked'] = true;
											}
											this._data[t ? 'core' : 'checkbox'].selected = this._data[
												t ? 'core' : 'checkbox'
											].selected.concat(m[dpc[i]].children_d);
										}
									}
								}
							}

							if (s.indexOf('up') !== -1) {
								// apply up
								for (i = 0, j = p.children_d.length; i < j; i++) {
									if (!m[p.children_d[i]].children.length) {
										chd.push(m[p.children_d[i]].parent);
									}
								}
								chd = $.vakata.array_unique(chd);
								for (k = 0, l = chd.length; k < l; k++) {
									p = m[chd[k]];
									while (p && p.id !== $.jstree.root) {
										c = 0;
										for (i = 0, j = p.children.length; i < j; i++) {
											c += m[p.children[i]].state[t ? 'selected' : 'checked'];
										}
										if (c === j) {
											p.state[t ? 'selected' : 'checked'] = true;
											this._data[t ? 'core' : 'checkbox'].selected.push(p.id);
											tmp = this.get_node(p, true);
											if (tmp && tmp.length) {
												tmp
													.attr('aria-selected', true)
													.children('.jstree-anchor')
													.addClass(t ? 'jstree-clicked' : 'jstree-checked');
											}
										} else {
											break;
										}
										p = this.get_node(p.parent);
									}
								}
							}

							this._data[t ? 'core' : 'checkbox'].selected = $.vakata.array_unique(
								this._data[t ? 'core' : 'checkbox'].selected
							);
						}, this)
					)
					.on(
						this.settings.checkbox.tie_selection ? 'select_node.jstree' : 'check_node.jstree',
						$.proxy(function (e, data) {
							var obj = data.node,
								m = this._model.data,
								par = this.get_node(obj.parent),
								dom = this.get_node(obj, true),
								i,
								j,
								c,
								tmp,
								s = this.settings.checkbox.cascade,
								t = this.settings.checkbox.tie_selection;

							// apply down
							if (s.indexOf('down') !== -1) {
								this._data[t ? 'core' : 'checkbox'].selected = $.vakata.array_unique(
									this._data[t ? 'core' : 'checkbox'].selected.concat(obj.children_d)
								);
								for (i = 0, j = obj.children_d.length; i < j; i++) {
									tmp = m[obj.children_d[i]];
									tmp.state[t ? 'selected' : 'checked'] = true;
									if (tmp && tmp.original && tmp.original.state && tmp.original.state.undetermined) {
										tmp.original.state.undetermined = false;
									}
								}
							}

							// apply up
							if (s.indexOf('up') !== -1) {
								while (par && par.id !== $.jstree.root) {
									c = 0;
									for (i = 0, j = par.children.length; i < j; i++) {
										c += m[par.children[i]].state[t ? 'selected' : 'checked'];
									}
									if (c === j) {
										par.state[t ? 'selected' : 'checked'] = true;
										this._data[t ? 'core' : 'checkbox'].selected.push(par.id);
										tmp = this.get_node(par, true);
										if (tmp && tmp.length) {
											tmp
												.attr('aria-selected', true)
												.children('.jstree-anchor')
												.addClass(t ? 'jstree-clicked' : 'jstree-checked');
										}
									} else {
										break;
									}
									par = this.get_node(par.parent);
								}
							}

							// apply down (process .children separately?)
							if (s.indexOf('down') !== -1 && dom.length) {
								dom
									.find('.jstree-anchor')
									.addClass(t ? 'jstree-clicked' : 'jstree-checked')
									.parent()
									.attr('aria-selected', true);
							}
						}, this)
					)
					.on(
						this.settings.checkbox.tie_selection ? 'deselect_all.jstree' : 'uncheck_all.jstree',
						$.proxy(function (e, data) {
							var obj = this.get_node($.jstree.root),
								m = this._model.data,
								i,
								j,
								tmp;
							for (i = 0, j = obj.children_d.length; i < j; i++) {
								tmp = m[obj.children_d[i]];
								if (tmp && tmp.original && tmp.original.state && tmp.original.state.undetermined) {
									tmp.original.state.undetermined = false;
								}
							}
						}, this)
					)
					.on(
						this.settings.checkbox.tie_selection ? 'deselect_node.jstree' : 'uncheck_node.jstree',
						$.proxy(function (e, data) {
							var obj = data.node,
								dom = this.get_node(obj, true),
								i,
								j,
								tmp,
								s = this.settings.checkbox.cascade,
								t = this.settings.checkbox.tie_selection;
							if (obj && obj.original && obj.original.state && obj.original.state.undetermined) {
								obj.original.state.undetermined = false;
							}

							// apply down
							if (s.indexOf('down') !== -1) {
								for (i = 0, j = obj.children_d.length; i < j; i++) {
									tmp = this._model.data[obj.children_d[i]];
									tmp.state[t ? 'selected' : 'checked'] = false;
									if (tmp && tmp.original && tmp.original.state && tmp.original.state.undetermined) {
										tmp.original.state.undetermined = false;
									}
								}
							}

							// apply up
							if (s.indexOf('up') !== -1) {
								for (i = 0, j = obj.parents.length; i < j; i++) {
									tmp = this._model.data[obj.parents[i]];
									tmp.state[t ? 'selected' : 'checked'] = false;
									if (tmp && tmp.original && tmp.original.state && tmp.original.state.undetermined) {
										tmp.original.state.undetermined = false;
									}
									tmp = this.get_node(obj.parents[i], true);
									if (tmp && tmp.length) {
										tmp
											.attr('aria-selected', false)
											.children('.jstree-anchor')
											.removeClass(t ? 'jstree-clicked' : 'jstree-checked');
									}
								}
							}
							tmp = [];
							for (i = 0, j = this._data[t ? 'core' : 'checkbox'].selected.length; i < j; i++) {
								// apply down + apply up
								if (
									(s.indexOf('down') === -1 ||
										$.inArray(this._data[t ? 'core' : 'checkbox'].selected[i], obj.children_d) === -1) &&
									(s.indexOf('up') === -1 ||
										$.inArray(this._data[t ? 'core' : 'checkbox'].selected[i], obj.parents) === -1)
								) {
									tmp.push(this._data[t ? 'core' : 'checkbox'].selected[i]);
								}
							}
							this._data[t ? 'core' : 'checkbox'].selected = $.vakata.array_unique(tmp);

							// apply down (process .children separately?)
							if (s.indexOf('down') !== -1 && dom.length) {
								dom
									.find('.jstree-anchor')
									.removeClass(t ? 'jstree-clicked' : 'jstree-checked')
									.parent()
									.attr('aria-selected', false);
							}
						}, this)
					);
			}
			if (this.settings.checkbox.cascade.indexOf('up') !== -1) {
				this.element
					.on(
						'delete_node.jstree',
						$.proxy(function (e, data) {
							// apply up (whole handler)
							var p = this.get_node(data.parent),
								m = this._model.data,
								i,
								j,
								c,
								tmp,
								t = this.settings.checkbox.tie_selection;
							while (p && p.id !== $.jstree.root && !p.state[t ? 'selected' : 'checked']) {
								c = 0;
								for (i = 0, j = p.children.length; i < j; i++) {
									c += m[p.children[i]].state[t ? 'selected' : 'checked'];
								}
								if (j > 0 && c === j) {
									p.state[t ? 'selected' : 'checked'] = true;
									this._data[t ? 'core' : 'checkbox'].selected.push(p.id);
									tmp = this.get_node(p, true);
									if (tmp && tmp.length) {
										tmp
											.attr('aria-selected', true)
											.children('.jstree-anchor')
											.addClass(t ? 'jstree-clicked' : 'jstree-checked');
									}
								} else {
									break;
								}
								p = this.get_node(p.parent);
							}
						}, this)
					)
					.on(
						'move_node.jstree',
						$.proxy(function (e, data) {
							// apply up (whole handler)
							var is_multi = data.is_multi,
								old_par = data.old_parent,
								new_par = this.get_node(data.parent),
								m = this._model.data,
								p,
								c,
								i,
								j,
								tmp,
								t = this.settings.checkbox.tie_selection;
							if (!is_multi) {
								p = this.get_node(old_par);
								while (p && p.id !== $.jstree.root && !p.state[t ? 'selected' : 'checked']) {
									c = 0;
									for (i = 0, j = p.children.length; i < j; i++) {
										c += m[p.children[i]].state[t ? 'selected' : 'checked'];
									}
									if (j > 0 && c === j) {
										p.state[t ? 'selected' : 'checked'] = true;
										this._data[t ? 'core' : 'checkbox'].selected.push(p.id);
										tmp = this.get_node(p, true);
										if (tmp && tmp.length) {
											tmp
												.attr('aria-selected', true)
												.children('.jstree-anchor')
												.addClass(t ? 'jstree-clicked' : 'jstree-checked');
										}
									} else {
										break;
									}
									p = this.get_node(p.parent);
								}
							}
							p = new_par;
							while (p && p.id !== $.jstree.root) {
								c = 0;
								for (i = 0, j = p.children.length; i < j; i++) {
									c += m[p.children[i]].state[t ? 'selected' : 'checked'];
								}
								if (c === j) {
									if (!p.state[t ? 'selected' : 'checked']) {
										p.state[t ? 'selected' : 'checked'] = true;
										this._data[t ? 'core' : 'checkbox'].selected.push(p.id);
										tmp = this.get_node(p, true);
										if (tmp && tmp.length) {
											tmp
												.attr('aria-selected', true)
												.children('.jstree-anchor')
												.addClass(t ? 'jstree-clicked' : 'jstree-checked');
										}
									}
								} else {
									if (p.state[t ? 'selected' : 'checked']) {
										p.state[t ? 'selected' : 'checked'] = false;
										this._data[t ? 'core' : 'checkbox'].selected = $.vakata.array_remove_item(
											this._data[t ? 'core' : 'checkbox'].selected,
											p.id
										);
										tmp = this.get_node(p, true);
										if (tmp && tmp.length) {
											tmp
												.attr('aria-selected', false)
												.children('.jstree-anchor')
												.removeClass(t ? 'jstree-clicked' : 'jstree-checked');
										}
									} else {
										break;
									}
								}
								p = this.get_node(p.parent);
							}
						}, this)
					);
			}
		};
		/**
		 * set the undetermined state where and if necessary. Used internally.
		 * @private
		 * @name _undetermined()
		 * @plugin checkbox
		 */
		this._undetermined = function () {
			if (this.element === null) {
				return;
			}
			var i,
				j,
				k,
				l,
				o = {},
				m = this._model.data,
				t = this.settings.checkbox.tie_selection,
				s = this._data[t ? 'core' : 'checkbox'].selected,
				p = [],
				tt = this;
			for (i = 0, j = s.length; i < j; i++) {
				if (m[s[i]] && m[s[i]].parents) {
					for (k = 0, l = m[s[i]].parents.length; k < l; k++) {
						if (o[m[s[i]].parents[k]] === undefined && m[s[i]].parents[k] !== $.jstree.root) {
							o[m[s[i]].parents[k]] = true;
							p.push(m[s[i]].parents[k]);
						}
					}
				}
			}
			// attempt for server side undetermined state
			this.element
				.find('.jstree-closed')
				.not(':has(.jstree-children)')
				.each(function () {
					var tmp = tt.get_node(this),
						tmp2;
					if (!tmp.state.loaded) {
						if (
							tmp.original &&
							tmp.original.state &&
							tmp.original.state.undetermined &&
							tmp.original.state.undetermined === true
						) {
							if (o[tmp.id] === undefined && tmp.id !== $.jstree.root) {
								o[tmp.id] = true;
								p.push(tmp.id);
							}
							for (k = 0, l = tmp.parents.length; k < l; k++) {
								if (o[tmp.parents[k]] === undefined && tmp.parents[k] !== $.jstree.root) {
									o[tmp.parents[k]] = true;
									p.push(tmp.parents[k]);
								}
							}
						}
					} else {
						for (i = 0, j = tmp.children_d.length; i < j; i++) {
							tmp2 = m[tmp.children_d[i]];
							if (
								!tmp2.state.loaded &&
								tmp2.original &&
								tmp2.original.state &&
								tmp2.original.state.undetermined &&
								tmp2.original.state.undetermined === true
							) {
								if (o[tmp2.id] === undefined && tmp2.id !== $.jstree.root) {
									o[tmp2.id] = true;
									p.push(tmp2.id);
								}
								for (k = 0, l = tmp2.parents.length; k < l; k++) {
									if (o[tmp2.parents[k]] === undefined && tmp2.parents[k] !== $.jstree.root) {
										o[tmp2.parents[k]] = true;
										p.push(tmp2.parents[k]);
									}
								}
							}
						}
					}
				});

			this.element.find('.jstree-undetermined').removeClass('jstree-undetermined');
			for (i = 0, j = p.length; i < j; i++) {
				if (!m[p[i]].state[t ? 'selected' : 'checked']) {
					s = this.get_node(p[i], true);
					if (s && s.length) {
						s.children('.jstree-anchor').children('.jstree-checkbox').addClass('jstree-undetermined');
					}
				}
			}
		};
		this.redraw_node = function (obj, deep, is_callback, force_render) {
			obj = parent.redraw_node.apply(this, arguments);
			if (this.get_node(obj).original.type == 'category') {
				return obj;
			}
			if (obj) {
				var i,
					j,
					tmp = null,
					icon = null;
				for (i = 0, j = obj.childNodes.length; i < j; i++) {
					if (
						obj.childNodes[i] &&
						obj.childNodes[i].className &&
						obj.childNodes[i].className.indexOf('jstree-anchor') !== -1
					) {
						tmp = obj.childNodes[i];
						break;
					}
				}

				if (tmp) {
					if (!this.settings.checkbox.tie_selection && this._model.data[obj.id].state.checked) {
						tmp.className += ' jstree-checked';
					}
					icon = _i.cloneNode(false);
					if (this._model.data[obj.id].state.checkbox_disabled) {
						icon.className += ' jstree-checkbox-disabled';
					}
					tmp.insertBefore(icon, tmp.childNodes[0]);
				}
			}
			if (!is_callback && this.settings.checkbox.cascade.indexOf('undetermined') !== -1) {
				if (this._data.checkbox.uto) {
					clearTimeout(this._data.checkbox.uto);
				}
				this._data.checkbox.uto = setTimeout($.proxy(this._undetermined, this), 50);
			}
			return obj;
		};
		/**
		 * show the node checkbox icons
		 * @name show_checkboxes()
		 * @plugin checkbox
		 */
		this.show_checkboxes = function () {
			this._data.core.themes.checkboxes = true;
			this.get_container_ul().removeClass('jstree-no-checkboxes');
		};
		/**
		 * hide the node checkbox icons
		 * @name hide_checkboxes()
		 * @plugin checkbox
		 */
		this.hide_checkboxes = function () {
			this._data.core.themes.checkboxes = false;
			this.get_container_ul().addClass('jstree-no-checkboxes');
		};
		/**
		 * toggle the node icons
		 * @name toggle_checkboxes()
		 * @plugin checkbox
		 */
		this.toggle_checkboxes = function () {
			if (this._data.core.themes.checkboxes) {
				this.hide_checkboxes();
			} else {
				this.show_checkboxes();
			}
		};
		/**
		 * checks if a node is in an undetermined state
		 * @name is_undetermined(obj)
		 * @param  {mixed} obj
		 * @return {Boolean}
		 */
		this.is_undetermined = function (obj) {
			obj = this.get_node(obj);
			var s = this.settings.checkbox.cascade,
				i,
				j,
				t = this.settings.checkbox.tie_selection,
				d = this._data[t ? 'core' : 'checkbox'].selected,
				m = this._model.data;
			if (
				!obj ||
				obj.state[t ? 'selected' : 'checked'] === true ||
				s.indexOf('undetermined') === -1 ||
				(s.indexOf('down') === -1 && s.indexOf('up') === -1)
			) {
				return false;
			}
			if (!obj.state.loaded && obj.original.state.undetermined === true) {
				return true;
			}
			for (i = 0, j = obj.children_d.length; i < j; i++) {
				if (
					$.inArray(obj.children_d[i], d) !== -1 ||
					(!m[obj.children_d[i]].state.loaded && m[obj.children_d[i]].original.state.undetermined)
				) {
					return true;
				}
			}
			return false;
		};
		/**
		 * disable a node's checkbox
		 * @name disable_checkbox(obj)
		 * @param {mixed} obj an array can be used too
		 * @trigger disable_checkbox.jstree
		 * @plugin checkbox
		 */
		this.disable_checkbox = function (obj) {
			var t1, t2, dom;
			if ($.isArray(obj)) {
				obj = obj.slice();
				for (t1 = 0, t2 = obj.length; t1 < t2; t1++) {
					this.disable_checkbox(obj[t1]);
				}
				return true;
			}
			obj = this.get_node(obj);
			if (!obj || obj.id === $.jstree.root) {
				return false;
			}
			dom = this.get_node(obj, true);
			if (!obj.state.checkbox_disabled) {
				obj.state.checkbox_disabled = true;
				if (dom && dom.length) {
					dom.children('.jstree-anchor').children('.jstree-checkbox').addClass('jstree-checkbox-disabled');
				}
				/**
				 * triggered when an node's checkbox is disabled
				 * @event
				 * @name disable_checkbox.jstree
				 * @param {Object} node
				 * @plugin checkbox
				 */
				this.trigger('disable_checkbox', { node: obj });
			}
		};
		/**
		 * enable a node's checkbox
		 * @name disable_checkbox(obj)
		 * @param {mixed} obj an array can be used too
		 * @trigger enable_checkbox.jstree
		 * @plugin checkbox
		 */
		this.enable_checkbox = function (obj) {
			var t1, t2, dom;
			if ($.isArray(obj)) {
				obj = obj.slice();
				for (t1 = 0, t2 = obj.length; t1 < t2; t1++) {
					this.enable_checkbox(obj[t1]);
				}
				return true;
			}
			obj = this.get_node(obj);
			if (!obj || obj.id === $.jstree.root) {
				return false;
			}
			dom = this.get_node(obj, true);
			if (obj.state.checkbox_disabled) {
				obj.state.checkbox_disabled = false;
				if (dom && dom.length) {
					dom.children('.jstree-anchor').children('.jstree-checkbox').removeClass('jstree-checkbox-disabled');
				}
				/**
				 * triggered when an node's checkbox is enabled
				 * @event
				 * @name enable_checkbox.jstree
				 * @param {Object} node
				 * @plugin checkbox
				 */
				this.trigger('enable_checkbox', { node: obj });
			}
		};

		this.activate_node = function (obj, e) {
			if ($(e.target).hasClass('jstree-checkbox-disabled')) {
				return false;
			}
			if (this.get_node(obj).original.type == 'category') {
				this.trigger('activate_node', { node: this.get_node(obj) });
			}
			if (
				this.settings.checkbox.tie_selection &&
				(this.settings.checkbox.whole_node || $(e.target).hasClass('jstree-checkbox'))
			) {
				e.ctrlKey = true;
			}

			if (
				this.settings.checkbox.tie_selection ||
				(!this.settings.checkbox.whole_node && !$(e.target).hasClass('jstree-checkbox'))
			) {
				return parent.activate_node.call(this, obj, e);
			}
			if (this.is_disabled(obj)) {
				return false;
			}
			if (this.is_checked(obj)) {
				this.uncheck_node(obj, e);
			} else {
				this.check_node(obj, e);
			}
			this.trigger('activate_node', { node: this.get_node(obj) });
		};

		/**
		 * check a node (only if tie_selection in checkbox settings is false, otherwise select_node will be called internally)
		 * @name check_node(obj)
		 * @param {mixed} obj an array can be used to check multiple nodes
		 * @trigger check_node.jstree
		 * @plugin checkbox
		 */
		this.check_node = function (obj, e) {
			if (this.settings.checkbox.tie_selection) {
				return this.select_node(obj, false, true, e);
			}
			var dom, t1, t2, th;
			if ($.isArray(obj)) {
				obj = obj.slice();
				for (t1 = 0, t2 = obj.length; t1 < t2; t1++) {
					this.check_node(obj[t1], e);
				}
				return true;
			}
			obj = this.get_node(obj);
			if (!obj || obj.id === $.jstree.root) {
				return false;
			}
			dom = this.get_node(obj, true);
			if (!obj.state.checked) {
				obj.state.checked = true;
				this._data.checkbox.selected.push(obj.id);
				if (dom && dom.length) {
					dom.children('.jstree-anchor').addClass('jstree-checked');
				}
				/**
				 * triggered when an node is checked (only if tie_selection in checkbox settings is false)
				 * @event
				 * @name check_node.jstree
				 * @param {Object} node
				 * @param {Array} selected the current selection
				 * @param {Object} event the event (if any) that triggered this check_node
				 * @plugin checkbox
				 */
				this.trigger('check_node', { node: obj, selected: this._data.checkbox.selected, event: e });
			}
		};
		/**
		 * uncheck a node (only if tie_selection in checkbox settings is false, otherwise deselect_node will be called internally)
		 * @name uncheck_node(obj)
		 * @param {mixed} obj an array can be used to uncheck multiple nodes
		 * @trigger uncheck_node.jstree
		 * @plugin checkbox
		 */
		this.uncheck_node = function (obj, e) {
			if (this.settings.checkbox.tie_selection) {
				return this.deselect_node(obj, false, e);
			}
			var t1, t2, dom;
			if ($.isArray(obj)) {
				obj = obj.slice();
				for (t1 = 0, t2 = obj.length; t1 < t2; t1++) {
					this.uncheck_node(obj[t1], e);
				}
				return true;
			}
			obj = this.get_node(obj);
			if (!obj || obj.id === $.jstree.root) {
				return false;
			}
			dom = this.get_node(obj, true);
			if (obj.state.checked) {
				obj.state.checked = false;
				this._data.checkbox.selected = $.vakata.array_remove_item(this._data.checkbox.selected, obj.id);
				if (dom.length) {
					dom.children('.jstree-anchor').removeClass('jstree-checked');
				}
				/**
				 * triggered when an node is unchecked (only if tie_selection in checkbox settings is false)
				 * @event
				 * @name uncheck_node.jstree
				 * @param {Object} node
				 * @param {Array} selected the current selection
				 * @param {Object} event the event (if any) that triggered this uncheck_node
				 * @plugin checkbox
				 */
				this.trigger('uncheck_node', {
					node: obj,
					selected: this._data.checkbox.selected,
					event: e
				});
			}
		};
		/**
		 * checks all nodes in the tree (only if tie_selection in checkbox settings is false, otherwise select_all will be called internally)
		 * @name check_all()
		 * @trigger check_all.jstree, changed.jstree
		 * @plugin checkbox
		 */
		this.check_all = function () {
			if (this.settings.checkbox.tie_selection) {
				return this.select_all();
			}
			this._data.checkbox.selected = this._model.data[$.jstree.root].children_d.concat();
			for (i = 0, j = this._data.checkbox.selected.length; i < j; i++) {
				if (this._model.data[this._data.checkbox.selected[i]]) {
					this._model.data[this._data.checkbox.selected[i]].state.checked = true;
				}
			}
			this.redraw(true);
			/**
			 * triggered when all nodes are checked (only if tie_selection in checkbox settings is false)
			 * @event
			 * @name check_all.jstree
			 * @param {Array} selected the current selection
			 * @plugin checkbox
			 */
			this.trigger('check_all', { selected: this._data.checkbox.selected });
		};
		/**
		 * uncheck all checked nodes (only if tie_selection in checkbox settings is false, otherwise deselect_all will be called internally)
		 * @name uncheck_all()
		 * @trigger uncheck_all.jstree
		 * @plugin checkbox
		 */
		this.uncheck_all = function () {
			if (this.settings.checkbox.tie_selection) {
				return this.deselect_all();
			}
			var tmp = this._data.checkbox.selected.concat([]),
				i,
				j;
			for (i = 0, j = this._data.checkbox.selected.length; i < j; i++) {
				if (this._model.data[this._data.checkbox.selected[i]]) {
					this._model.data[this._data.checkbox.selected[i]].state.checked = false;
				}
			}
			this._data.checkbox.selected = [];
			this.element.find('.jstree-checked').removeClass('jstree-checked');
			/**
			 * triggered when all nodes are unchecked (only if tie_selection in checkbox settings is false)
			 * @event
			 * @name uncheck_all.jstree
			 * @param {Object} node the previous selection
			 * @param {Array} selected the current selection
			 * @plugin checkbox
			 */
			this.trigger('uncheck_all', { selected: this._data.checkbox.selected, node: tmp });
		};
		/**
		 * checks if a node is checked (if tie_selection is on in the settings this function will return the same as is_selected)
		 * @name is_checked(obj)
		 * @param  {mixed}  obj
		 * @return {Boolean}
		 * @plugin checkbox
		 */
		this.is_checked = function (obj) {
			if (this.settings.checkbox.tie_selection) {
				return this.is_selected(obj);
			}
			obj = this.get_node(obj);
			if (!obj || obj.id === $.jstree.root) {
				return false;
			}
			return obj.state.checked;
		};
		/**
		 * get an array of all checked nodes (if tie_selection is on in the settings this function will return the same as get_selected)
		 * @name get_checked([full])
		 * @param  {mixed}  full if set to `true` the returned array will consist of the full node objects, otherwise - only IDs will be returned
		 * @return {Array}
		 * @plugin checkbox
		 */
		this.get_checked = function (full) {
			if (this.settings.checkbox.tie_selection) {
				return this.get_selected(full);
			}
			return full
				? $.map(
						this._data.checkbox.selected,
						$.proxy(function (i) {
							return this.get_node(i);
						}, this)
				  )
				: this._data.checkbox.selected;
		};
		/**
		 * get an array of all top level checked nodes (ignoring children of checked nodes) (if tie_selection is on in the settings this function will return the same as get_top_selected)
		 * @name get_top_checked([full])
		 * @param  {mixed}  full if set to `true` the returned array will consist of the full node objects, otherwise - only IDs will be returned
		 * @return {Array}
		 * @plugin checkbox
		 */
		this.get_top_checked = function (full) {
			if (this.settings.checkbox.tie_selection) {
				return this.get_top_selected(full);
			}
			var tmp = this.get_checked(true),
				obj = {},
				i,
				j,
				k,
				l;
			for (i = 0, j = tmp.length; i < j; i++) {
				obj[tmp[i].id] = tmp[i];
			}
			for (i = 0, j = tmp.length; i < j; i++) {
				for (k = 0, l = tmp[i].children_d.length; k < l; k++) {
					if (obj[tmp[i].children_d[k]]) {
						delete obj[tmp[i].children_d[k]];
					}
				}
			}
			tmp = [];
			for (i in obj) {
				if (obj.hasOwnProperty(i)) {
					tmp.push(i);
				}
			}
			return full
				? $.map(
						tmp,
						$.proxy(function (i) {
							return this.get_node(i);
						}, this)
				  )
				: tmp;
		};
		/**
		 * get an array of all bottom level checked nodes (ignoring selected parents) (if tie_selection is on in the settings this function will return the same as get_bottom_selected)
		 * @name get_bottom_checked([full])
		 * @param  {mixed}  full if set to `true` the returned array will consist of the full node objects, otherwise - only IDs will be returned
		 * @return {Array}
		 * @plugin checkbox
		 */
		this.get_bottom_checked = function (full) {
			if (this.settings.checkbox.tie_selection) {
				return this.get_bottom_selected(full);
			}
			var tmp = this.get_checked(true),
				obj = [],
				i,
				j;
			for (i = 0, j = tmp.length; i < j; i++) {
				if (!tmp[i].children.length) {
					obj.push(tmp[i].id);
				}
			}
			return full
				? $.map(
						obj,
						$.proxy(function (i) {
							return this.get_node(i);
						}, this)
				  )
				: obj;
		};
		this.load_node = function (obj, callback) {
			var k, l, i, j, c, tmp;
			if (!$.isArray(obj) && !this.settings.checkbox.tie_selection) {
				tmp = this.get_node(obj);
				if (tmp && tmp.state.loaded) {
					for (k = 0, l = tmp.children_d.length; k < l; k++) {
						if (this._model.data[tmp.children_d[k]].state.checked) {
							this._data.checkbox.selected = $.vakata.array_remove_item(
								this._data.checkbox.selected,
								tmp.children_d[k]
							);
						}
					}
				}
			}
			return parent.load_node.apply(this, arguments);
		};
		this.get_state = function () {
			var state = parent.get_state.apply(this, arguments);
			if (this.settings.checkbox.tie_selection) {
				return state;
			}
			state.checkbox = this._data.checkbox.selected.slice();
			return state;
		};
		this.set_state = function (state, callback) {
			var res = parent.set_state.apply(this, arguments);
			if (res && state.checkbox) {
				if (!this.settings.checkbox.tie_selection) {
					this.uncheck_all();
					var _this = this;
					$.each(state.checkbox, function (i, v) {
						_this.check_node(v);
					});
				}
				delete state.checkbox;
				this.set_state(state, callback);
				return false;
			}
			return res;
		};
		this.refresh = function (skip_loading, forget_state) {
			if (!this.settings.checkbox.tie_selection) {
				this._data.checkbox.selected = [];
			}
			return parent.refresh.apply(this, arguments);
		};
	};

	// include the checkbox plugin by default
	// $.jstree.defaults.plugins.push("checkbox");
});
